/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.domain.User;
import com.forms.ChangePasswordForm;
import com.forms.ForwardForm;

public class SaveChangePasswordAction extends CommunityBaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);

		// Cast the actionForm, if there is one.
		ChangePasswordForm f = (ChangePasswordForm) form;
		User user = (User)request.getSession().getAttribute("AUTH_USER");
		//Checking user
		if(user != null){
			//Check password
			if(f.getPassword1().equals(f.getPassword2())){
				user.setPassword(f.getPassword1());
				user = this.biz.getPrivateMgr().saveUserPassword(user,f.getPassword1());
				request.getSession().setAttribute("AUTH_USER", user);
				request.setAttribute("INFO", "Password successfully changed.");
			}
		}
		else{
			request.setAttribute("ERROR", ("Problem with saving password for user: "+user.getUsername()));
			return mapping.findForward("failure");
		}
		

		return mapping.findForward("success");//forward;
	}
}
